Blog

Curt Gratz

March 19, 2010

Spread the word


Share your thoughts

MockBox can have many amazing uses.  One well documented use is in assistance in creating complicated unit tests insuring you are only testing one small unit of work, however it can be used for many other interesting use cases.

One of the ways we find MockBox useful during our development cycle at Computer Know How is to mock objects we haven't had the time to complete yet, but we do know what we expect as response.  This allows us to continue development without waiting for the piece of something we haven't done yet slow us down, but keep the method calls exactly as they will be in when the object is complete.

So, how do you use MockBox outside of the context of a unit test?  Well, its easy.  

Inside a ColdBox application.

//get an instance of mockbox to use for mocking things not 100% built yet inside a ColdBox app mockBox = createObject("component","coldbox.system.testing.MockBox").init();

Outside a ColdBox application.

//get an instance of mockbox to use for mocking things not 100% built yet outside a ColdBox app. mockBox = createObject("component","mockbox.system.testing.MockBox").init();

Now that MockBox is initialized, we can start mocking objects. Lets say we have a User Object that we haven't had time to build yet. Right now it looks really advanced with lots of cool properties and functions that we spent a ton of time on. Something like this.

cfcomponent hint="I am the User ORM cfc" /cfcomponent

Now what if we needed to build a Welcome screen based off this User Object. Well, we could do something like this.

var rc = event.getCollection(); //get the users name, later we will need to switch this to get it from the user object. rc.Name = "Paul Was Saul"; But later, we would have to implement our user object once we are done with it. But what if we mocked our user object with the getName method it will ultimately have. var rc = event.getCollection(); //get an instance of mockbox var mockBox = createObject("component","coldbox.system.testing.MockBox").init() //Mock a User Object since its not built yet. var User=mockBox.createMock(className="model.ORM.User",clearMethods=true,callLogging=true); User.$(method="getName",returns="Paul Was Saul"); //get the users name rc.Name = User.getName();

Now, I know what your thinking. The non-mocked version looks a lot simpler then the mocked version. Well that is probably true in this simple example, but use your imagination and expand this example using the power of your mind and you can see how handy MockBox can be in your own development.

Want to learn more about MockBox. Read the wiki. As with everything done by Luis Majano, the documentation is outstanding.

http://wiki.coldbox.org/wiki/MockBox.cfm

Or Download it at

http://www.coldbox.org/download

Yet another tool to put in your toolbox to make your life simpler brought to you by Team ColdBox.

Add Your Comment

(2)

Mar 19, 2010 10:53:11 UTC

by Luis Majano

Cool approach Curt, My head started spinning and could be cool to have a mock interceptor that sets up all the mocks you would need for your session until objects get build out. This way you can put all your mock code in this single object and be coding it out. Very interesting approach. PS: you don't need to do: rc = event.getCollection() on your views/layouts. That's done implicitly :)

Mar 19, 2010 10:58:59 UTC

by Curt Gratz

Thanks man. In reality I do it in the mocks preHandler interceptor so its available to me when I need it. PS: those are are meant to be handlers, I don't think it does it implicitly there.

Recent Entries

Partner with BoxLang and Ortus at Into the Box 2025: Empowering the Future of Modern Software Development!

Partner with BoxLang and Ortus at Into the Box 2025: Empowering the Future of Modern Software Development!

At Ortus Solutions, we’ve always been at the forefront of innovation in the ColdFusion ecosystem. From pioneering modern ColdFusion practices to developing cutting-edge tools and frameworks, we’ve been passionate to help and sup[port the community into shaping the future of web development.That’s why we decided to build BoxLang, our new JVM programming language that not only builds on the strengths of ColdFusion but takes modern software development to the next level.

Maria Jose Herrera
Maria Jose Herrera
December 23, 2024
Why BoxLang When You Have Kotlin, Groovy, Scala, and more…

Why BoxLang When You Have Kotlin, Groovy, Scala, and more…

As we approach a stable release of BoxLang and our continued marketing reaches more folks, many have asked about its purpose. Why create a new language when the JVM ecosystem already includes established languages like Kotlin, Groovy, and Scala, to name a few.

Luis Majano
Luis Majano
December 18, 2024